/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iqq.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JToolBar;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import iqq.util.Log;
import iqq.util.QQImageUtil;

/**
 *
 * @author chenzhihui
 */
public class QQFaceDialog extends javax.swing.JDialog {

    private Map map = MainFrame.getMainFrame().getMap();
    private ChatDialog chatDialog = (ChatDialog) map.get("chatDialog");
    private static QQFaceDialog qqFaceDialog;
    private JEditorPane editor;

    /**
     * Creates new form QQFaceDialog
     */
    public static QQFaceDialog getInstance() {
        if (qqFaceDialog == null) {
            qqFaceDialog = new QQFaceDialog((ChatDialog) MainFrame.getMainFrame().getMap().get("chatDialog"), false);
        }
        return qqFaceDialog;
    }

    public QQFaceDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        //defautFacePanel.removeAll();
        //表情加载
        int i = 0;
        JButton[] face = new JButton[105];
        for (int t = 0; t < 7; t++) {
            JToolBar toolBar = new JToolBar();
            toolBar.setFloatable(false);
            toolBar.setRollover(true);
            for (int j = 0; j < 15; j++, i++) {
                face[i] = new JButton();
                face[i].setToolTipText("~face:" + i + "~");
                face[i].setIcon(new ImageIcon(getClass().getResource("/iqq/res/images/face/" + i + ".gif"))); // NOI18N
                face[i].setFocusable(false);
                face[i].setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
                face[i].setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
                toolBar.add(face[i]);

                //表情按钮事件
                face[i].addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();

                        Document doc = editor.getDocument();
                        try {
                            doc.insertString(editor.getSelectionEnd(), btn.getToolTipText(), null);
                        } catch (BadLocationException ex) {
                            Logger.getLogger(QQFaceDialog.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        String tipText = btn.getToolTipText();
                        Log.println(tipText + " flag:" + tipText.substring(6, tipText.lastIndexOf("~")));
                        editor.setText(editor.getText().replaceAll(tipText, QQImageUtil.flagToHTML(tipText.substring(6, tipText.lastIndexOf("~")))));
                        setVisible(false);
                    }
                });
            }
            defautFacePanel.add(toolBar);
            if (i == 104) {
                break;
            }
        }
        this.validate();
        this.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        defautFacePanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setUndecorated(true);

        jTabbedPane1.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);

        defautFacePanel.setLayout(new java.awt.GridLayout(7, 1));
        jTabbedPane1.addTab("默认", defautFacePanel);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 266, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("自定义表情", jPanel2);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(QQFaceDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(QQFaceDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(QQFaceDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QQFaceDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                QQFaceDialog dialog = new QQFaceDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel defautFacePanel;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

    void setTextEditor(JEditorPane editor) {
        this.editor = editor;
    }
}
